/*后台JS函数库*/

var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function Q(id) {
    return document.getElementById(id);
}
    
function slideToggle(div){
    $('#'+div).slideToggle();
}

function checkAll(type, form, value, checkall, changestyle) {
    var checkall = checkall ? checkall : 'chkall';
    for(var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if(type == 'option' && e.type == 'radio' && e.value == value && e.disabled != true) {
            e.checked = true;
        } else if(type == 'value' && e.type == 'checkbox' && e.value == value) {
            e.checked = form.elements[checkall].checked;
        } else if(type == 'prefix' && e.name && e.name != checkall && (!value || (value && e.name.match(value)))) {
            e.checked = form.elements[checkall].checked;
            if(changestyle && e.parentNode && e.parentNode.tagName.toLowerCase() == 'li') {
                e.parentNode.className = e.checked ? 'checked' : '';
            }
        }
    }
}

function altStyle(obj) {
    function altStyleClear(obj) {
        var input, lis, i;
        lis = obj.parentNode.getElementsByTagName('li');
        for(i=0; i < lis.length; i++){
            lis[i].className = '';
        }
    }

    var input, lis, i, cc, o;
    cc = 0;
    lis = obj.getElementsByTagName('li');
    for(i=0; i < lis.length; i++){
        lis[i].onclick = function(e) {
            o = is_ie ? event.srcElement.tagName : e.target.tagName;
            if(cc) {
                return;
            }
            cc = 1;
            input = this.getElementsByTagName('input')[0];
            if(input.getAttribute('type') == 'checkbox' || input.getAttribute('type') == 'radio') {
                if(input.getAttribute('type') == 'radio') {
                    altStyleClear(this);
                }

                if(is_ie || o != 'INPUT' && input.onclick) {
                    input.click();
                }
                if(this.className != 'checked') {
                    this.className = 'checked';
                    input.checked = true;
                } else {
                    this.className = '';
                    input.checked = false;
                }
            }
        }
        lis[i].onmouseup = function(e) {
            cc = 0;
        }
    }
}

function textareasize(obj) {
    if(obj.scrollHeight > 70) {
        obj.style.height = obj.scrollHeight + 'px';
    }
}

function showanchor(obj) {
    var navs = Q('submenu').getElementsByTagName('li');
    for(var i = 0; i < navs.length; i++) {
        if(navs[i].id != obj.id) {
            navs[i].className = '';
            Q(navs[i].id.substr(4)).style.display = 'none';
            if(Q(navs[i].id.substr(4) + '_tips')) Q(navs[i].id.substr(4) + '_tips').style.display = 'none';
        }
    }
    obj.className = 'current';
    Q(obj.id.substr(4)).style.display = '';
    if(Q(obj.id.substr(4) + '_tips')) Q(obj.id.substr(4) + '_tips').style.display = '';
    if(Q(obj.id.substr(4) + 'form')) {
        Q(obj.id.substr(4) + 'form').anchor.value = obj.id.substr(4);
    } else if(Q('cpform')) {
        Q('cpform').anchor.value = obj.id.substr(4);
    }
}

var addrowdirect = 0;
function addrow(obj, type) {
    var table = obj.parentNode.parentNode.parentNode.parentNode;
    if(!addrowdirect) {
        var row = table.insertRow(obj.parentNode.parentNode.parentNode.rowIndex);
    } else {
        var row = table.insertRow(obj.parentNode.parentNode.parentNode.rowIndex + 1);
    }
    var typedata = rowtypedata[type];
    for(var i = 0; i <= typedata.length - 1; i++) {
        var cell = row.insertCell(i);
        cell.colSpan = typedata[i][0];
        var tmp = typedata[i][1];
        if(typedata[i][2]) {
            cell.className = typedata[i][2];
        }
        tmp = tmp.replace('@', addrow.arguments[2]);
        cell.innerHTML = tmp;
    }
    addrowdirect = 0;
}

function delSubCate(obj) {
    var _parentRow = obj.parentNode.parentNode.parentNode.parentNode;
    var thisRow = obj.parentNode.parentNode.parentNode;
    _parentRow.removeChild(thisRow);
}

function delTopCate(obj) {
    var _parentRow = obj.parentNode.parentNode.parentNode;
    var thisRow = obj.parentNode.parentNode;
    _parentRow.removeChild(thisRow);
}

function dropmenu(obj){
    obj.className = obj.className == 'hasdropmenu' ? 'current' : 'hasdropmenu';
    Q(obj.id + 'child').style.display = Q(obj.id + 'child').style.display == 'none' ? '' : 'none';
}

function captcha(id){
    var now = new Date().getTime();
    var verify = Q(id);
    verify.src = APP + '/Admin/Public/captcha/' + now;
}

function ipToCountry(ip){
    $.blockUI({message : 'Processing ...'});
    var url = APP + '/Admin/Ajax/ipToCountry/ip/' + ip;    
    $.post(url,function(data){
        $.blockUI({message : data});
        $('.blockOverlay').attr('title','Click to Unblock').click($.unblockUI); 
    });
}

function showBanReason(obj, div){
	var id = $(obj).val();
	var url = APP + '/Admin/Ajax/getBanReason';
	var data = {id: id};
	if(id != '0'){
		$.post(url, data, function(json){
			var json = eval('('+ json +')');
			$('#'+div).html(json.reason);
		});
	}
}